package ntd.action;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.log.StaticLog;
import ntd.action.bo.ActionInBo;
import ntd.action.bo.ActionOutBo;

public abstract class ActionAbs implements Action {

    protected ActionInBo actionInBo;

    @Override
    public final void putParam(ActionInBo actionInBo) {
        this.actionInBo = actionInBo;
    }

    @Override
    public final ActionOutBo execute(ActionInBo actionInBo) {
        this.putParam(actionInBo);
        return this.execute();
    }

    @Override
    public final ActionOutBo execute() {
        try {
            TimeInterval timer = DateUtil.timer();
            Object handle = this.handle();
            if (timer.interval() > 1000) {
                StaticLog.error("动作[{}]执行慢[{}]", info().getActionCode(), timer.interval());
            }

            // 包装 ActionOutBO
            if (handle instanceof ActionOutBo) {
                return (ActionOutBo) handle;
            } else {
                return new ActionOutBo(handle);
            }
        } catch (Exception e) {
            e.printStackTrace();
            StaticLog.error("动作[{}]执行失败，参数[{}]", info().getActionCode(), actionInBo);
            throw e;
        }
    }

    public abstract ActionOutBo handle();
}
